package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;

import Person_JavaBean.User;
import Person_JavaBean.server;


public class Servlet01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        server s= new server();
        String name = request.getParameter("name");
        try {
            User jsList = s.json(name);
            response.setContentType("text/json;charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
//            out.println("name:"+jsList.getName());
            out.println(jsList.getBiolography());
            Cookie cookie0 = new Cookie("biolography", URLEncoder.encode(jsList.getBiolography(),"utf-8"));
            response.addCookie(cookie0);
            Cookie cookie1 = new Cookie("defence",String.valueOf(jsList.getDefence()));
            response.addCookie(cookie1);
            Cookie cookie2 = new Cookie("force",String.valueOf(jsList.getForc()));
            response.addCookie(cookie2);
            Cookie cookie3 = new Cookie("inteligence",String.valueOf(jsList.getInteligence()));
            response.addCookie(cookie3);
            Cookie cookie4 = new Cookie("politics",String.valueOf(jsList.getPolitics()));
            response.addCookie(cookie4);
            Cookie cookie5 = new Cookie("charm",String.valueOf(jsList.getCharm()));
            response.addCookie(cookie5);
//            InputStream in = jsList.getPhoto();
//            String file_path = "C:\\Users\\86139\\WeChatProjects\\血色人物图鉴\\pages\\images\\result_photo\\"+name+".png";
//            File file = new File(file_path);
//            String path = file_path.substring(0, file_path.lastIndexOf("\\"));
//            if (!file.exists()) {
//                new File(path).mkdir();
//            }
//            FileOutputStream fos = null;
//            try{
//                fos = new FileOutputStream(file);
//                int len = 0;
//                byte[] buf = new byte[1024];
//                while ((len = in.read(buf)) != -1) {
//                    fos.write(buf, 0, len);
//                }
//                fos.flush();
//            } catch (Exception e){
//                e.printStackTrace();
//            }finally {
//                if (null != fos) {
//                    try {
//                        fos.close();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
